#include <stdio.h>
#include <math.h>
#include "app_config.h"

#include "esp_mac.h"
#include "esp_app_desc.h"
#include "esp_heap_caps.h"
#include "api_config.h"

#include "wifi.h"

extern void call_reboot(void);

/* 👇👇👇👇👇👇 配置选项 👇👇👇👇👇👇 */
extern config_item_s wifi_config_item;
extern config_item_s ota_config_item;
extern config_item_s display_config_item;
extern config_item_s bat_config_item;
extern config_item_s afe_config_item;
extern config_item_s timer_config_item;
static config_item_s reboot_item = {
    .name = "重启",
    .enter = (config_item_cb_t)call_reboot,
};

static config_item_t set_list[] = {
    &wifi_config_item,
    &ota_config_item,
    &display_config_item,
    &bat_config_item,
    &afe_config_item,
    &timer_config_item,
    &reboot_item,
};
#define SET_LIST_COUNT (sizeof(set_list) / sizeof(set_list[0]))
/* 👆👆👆👆👆👆 新增页面 👆👆👆👆👆👆 */

/*
    1. 信息页 - 首页 - 长按进入配置页
    2. 显示当前版本号
    3. 显示当前固件大小
    4. 显示当前固件MD5
    5. 显示当前固件版本号
    6. 显示当前固件发布日期
    7. 显示当前固件作者
    8. 显示当前固件描述
    9. 显示当前固件网址

    配置页
    1. WiFi 选项 - 1.配网模式 2.状态及信息
    2. OTA 升级选项 - 1. 检查版本 2. 升级(如果有新版本) 3. 回退版本(如果存在)
    3. 屏幕 选项
    4. 电池 选项
    5. AFE 选项
    6. 定时功能 - 定时开/关
    7. 重启
*/

static bool enter_sub_page = false;
static bool enter_item_sub_page = false;
static bool wifi = false;
static lv_timer_t *item_timer = NULL;
static int8_t cur_select = -1;

static void list_select_item(lv_obj_t *page, uint8_t id)
{
    lv_obj_t *set_page = lv_obj_get_child(page, 1);
    uint8_t cnt = lv_obj_get_child_cnt(set_page);
    for (uint8_t i = 0; i < cnt; i++)
    {
        lv_obj_t *cur_item = lv_obj_get_child(set_page, i);
        lv_obj_set_style_border_color(cur_item, lv_color_make(64, 64, 64), LV_PART_MAIN);
        lv_obj_set_style_text_color(cur_item, lv_color_make(255, 255, 255), LV_PART_MAIN);
    }
    lv_obj_t *item = lv_obj_get_child(set_page, id);
    if (!item)
        return;
    lv_obj_set_style_text_color(item, lv_color_make(255, 165, 2), LV_PART_MAIN);
    // lv_obj_set_style_border_color(item, lv_color_make(255, 165, 2), LV_PART_MAIN);
    lv_obj_scroll_to_view(item, 1);
    cur_select = id;
}

static void timer_task_cb(struct _lv_timer_t *timer)
{
    if (enter_sub_page)
        return;
    lv_obj_t *obj = timer->user_data;
    if (wifi_get_localIP())
    {
        lv_obj_t *info_page = lv_obj_get_child(obj, 0);
        lv_obj_t *label = lv_obj_get_child(info_page, 0);
        lv_obj_t *wifi_ico = lv_obj_get_child(label, 0);
        lv_obj_t *rssi = lv_obj_get_child(label, 1);
        wifi_ap_record_t ap_info = {0};
        esp_wifi_sta_get_ap_info(&ap_info);
        if (!wifi)
        {
            wifi = true;
            lv_label_set_text_static(wifi_ico, LV_SYMBOL_WIFI);
            lv_obj_align_to(wifi_ico, label, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
        }
        lv_label_set_text_fmt(rssi, "%d", ap_info.rssi);
        lv_obj_align_to(rssi, wifi_ico, LV_ALIGN_OUT_RIGHT_MID, 2, 0);
    }
    else if (wifi)
    {
        wifi = false;
        lv_obj_t *info_page = lv_obj_get_child(obj, 0);
        lv_obj_t *label = lv_obj_get_child(info_page, 0);
        lv_obj_t *wifi_ico = lv_obj_get_child(label, 0);
        lv_obj_t *rssi = lv_obj_get_child(label, 1);
        lv_label_set_text_static(wifi_ico, " ");
        lv_label_set_text_static(rssi, " ");
    }
}

lv_timer_t *app_config_create(lv_obj_t *parent, uint8_t id)
{
    if (!parent)
        return NULL;

    lv_obj_t *obj = lv_tileview_add_tile(parent, id, 0, LV_DIR_HOR);
    lv_obj_set_style_pad_all(obj, 10, LV_PART_MAIN);
    lv_obj_set_style_text_font(obj, base_font18, LV_PART_MAIN);
    lv_obj_set_style_bg_color(obj, lv_color_make(0, 0, 0), LV_PART_MAIN);

    lv_obj_t *info_page = lv_obj_create(obj);
    lv_obj_set_size(info_page, LV_PCT(100), LV_PCT(100));
    lv_obj_set_style_radius(info_page, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_all(info_page, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_row(info_page, 5, LV_PART_MAIN);
    lv_obj_set_flex_flow(info_page, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_border_width(info_page, 0, LV_PART_MAIN);
    lv_obj_set_scrollbar_mode(info_page, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_style_bg_opa(info_page, LV_OPA_50, LV_PART_MAIN);
    lv_obj_set_style_text_font(info_page, base_font12, LV_PART_MAIN);
    lv_obj_set_style_bg_color(info_page, lv_color_make(0, 0, 0), LV_PART_MAIN);
    lv_obj_set_style_text_color(info_page, lv_color_make(255, 255, 255), LV_PART_MAIN);

    uint8_t macs[6] = {0};
    const esp_app_desc_t *app_desc = esp_app_get_description();
    esp_base_mac_addr_get(macs);

    lv_obj_t *label, *wifi_ico;
    label = lv_label_create(info_page);
    lv_label_set_text_fmt(label, "软件版本 : %s   API 版本 : " API_VERSION, app_desc->version);

    wifi_ico = lv_label_create(label);
    lv_label_set_text_static(wifi_ico, " ");
    lv_obj_set_style_text_font(wifi_ico, &lv_font_montserrat_14, LV_PART_MAIN);
    lv_obj_align(wifi_ico, LV_ALIGN_RIGHT_MID, 0, 0);

    wifi_ico = lv_label_create(label);
    lv_label_set_text_static(wifi_ico, " ");

    label = lv_label_create(info_page);
    lv_label_set_text_fmt(label, "芯片 : " chip_name " 编译时间 : %s", app_desc->time);
    label = lv_label_create(info_page);
    lv_label_set_text_fmt(label, "MAC : " MACSTR, MAC2STR(macs));
    label = lv_label_create(info_page);
    lv_label_set_text_static(label, "项目开发者 : maplerian");
    label = lv_label_create(info_page);
    lv_obj_set_width(label, LV_PCT(100));
    lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_label_set_text_static(label, "固件开源地址 : gitee.com/maplerian/esp-bat-pb-idf");
    label = lv_label_create(info_page);
    lv_obj_set_width(label, LV_PCT(100));
    lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_label_set_text_static(label, "项目开源地址 : oshwhub.com/maplerian/esp-battery-protection-board");

    /*
        配置页
        1. WiFi 选项 - 1.配网模式 2.状态及信息
        2. OTA 升级选项 - 1. 检查版本 2. 升级(如果有新版本) 3. 回退版本(如果存在)
        3. 屏幕 选项
        4. 电池 选项
        5. AFE 选项
        6. 定时功能 - 定时开/关
        7. 重启
     */
    lv_obj_t *set_page = lv_list_create(obj);
    lv_obj_set_size(set_page, LV_PCT(100), LV_PCT(100));
    lv_obj_set_style_radius(set_page, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(set_page, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_row(set_page, 0, LV_PART_MAIN);
    lv_obj_set_flex_flow(set_page, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_bg_color(set_page, lv_color_make(0, 0, 0), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(set_page, LV_OPA_50, LV_PART_MAIN);
    lv_obj_set_scrollbar_mode(set_page, LV_SCROLLBAR_MODE_AUTO);
    lv_obj_add_flag(set_page, LV_OBJ_FLAG_HIDDEN);

    for (uint8_t i = 0; i < SET_LIST_COUNT; i++)
    {
        lv_obj_t *label = lv_list_add_text(set_page, set_list[i]->name);
        lv_obj_set_style_pad_ver(label, 5, LV_PART_MAIN);
        lv_obj_set_style_bg_color(label, lv_color_make(64, 64, 64), LV_PART_MAIN);
        lv_obj_set_style_text_color(label, lv_color_make(255, 255, 255), LV_PART_MAIN);
        lv_obj_set_style_border_color(label, lv_color_make(64, 64, 64), LV_PART_MAIN);
        lv_obj_set_style_border_width(label, 1, LV_PART_MAIN);
    }

    lv_timer_t *timer = lv_timer_create(timer_task_cb, 1000, obj);
    lv_timer_pause(timer);
    return timer;
}

void config_enter(lv_timer_t *timer, void *user_data)
{
    lv_obj_t *obj = timer->user_data;
    lv_obj_t *info_page = lv_obj_get_child(obj, 0);
    lv_obj_t *label = lv_obj_get_child(info_page, 4);
    lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
    label = lv_obj_get_child(info_page, 5);
    lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
}

void config_level(lv_timer_t *timer, void *user_data)
{
    lv_obj_t *obj = timer->user_data;
    lv_obj_t *info_page = lv_obj_get_child(obj, 0);
    lv_obj_t *label = lv_obj_get_child(info_page, 4);
    lv_label_set_long_mode(label, LV_LABEL_LONG_CLIP);
    label = lv_obj_get_child(info_page, 5);
    lv_label_set_long_mode(label, LV_LABEL_LONG_CLIP);
}

uint8_t config_update_cb(lv_timer_t *timer, void *user_data, uint8_t event)
{
    lv_obj_t *obj = timer->user_data;
    lv_obj_t *info_page = lv_obj_get_child(obj, 0);
    lv_obj_t *set_page = lv_obj_get_child(obj, 1);
    config_item_t item = set_list[cur_select];
    if (enter_item_sub_page)
    {
        if (item->update)
            enter_item_sub_page = item->update(item_timer, item->user_data, event);
        return 1;
    }
    if (event == APP_EVENT_TYPE_NEXT)
    {
        if (enter_sub_page)
        {
            if (cur_select == -1)
                return 0;
            uint8_t idx = cur_select + 1;
            if (idx >= SET_LIST_COUNT)
                idx = 0;
            if (idx != cur_select)
            {
                cur_select = idx;
                list_select_item(obj, cur_select);
            }
        }
        return 1;
    }
    else if (event == APP_EVENT_TYPE_CLICK)
    {
        if (enter_sub_page)
        {
            if (item->enter)
            {
                item_timer = item->enter(obj);
                enter_item_sub_page = true;
            }
        }
        return 1;
    }
    else if (event == APP_EVENT_TYPE_CONFIRM) /* 进入 / 退出页面 */
    {
        if (enter_sub_page)
        {
            enter_sub_page = false;
            lv_obj_t *item = lv_obj_get_child(set_page, cur_select);
            lv_obj_set_style_border_color(item, lv_color_make(64, 64, 64), LV_PART_MAIN);
            lv_obj_set_style_text_color(item, lv_color_make(255, 255, 255), LV_PART_MAIN);

            lv_obj_add_flag(set_page, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(info_page, LV_OBJ_FLAG_HIDDEN);
        }
        else
        {
            enter_sub_page = true;
            lv_obj_add_flag(info_page, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(set_page, LV_OBJ_FLAG_HIDDEN);
            list_select_item(obj, 0);
        }
        return enter_sub_page;
    }
    return 0;
}

app_s config_page = {
    .name = "config",
    .create = app_config_create,
    .delete = NULL,
    .enter = config_enter,
    .level = config_level,
    .update = config_update_cb,
    .user_data = NULL,
};
